<?php
namespace app\api\controller;
use x\Xbase;
use x\G;
use x\Container;
class Index extends Xbase{

    public function init(){
        G::$exts['api_version'] = 1;
        // 准许跨域请求。
        header("Access-Control-Allow-Origin: * ");
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        if(is_post()){
            if(!$this->checkSign())
                err('签名错误');
            $d = $this->post;
            log_test($d['url'], $d);

            //调用接口
            $arr = array_merge(array_filter(explode("/",$d['url'])),[]);
            $arr[1] = ucfirst($arr[1]);
            $action = end($arr);
            $class = str_replace("/","\\","app\api\controller\\".$arr[0]."\\".$arr[1]);
            $container = Container::getInstance();
            $class = $container->make($class);
            $container->invokeMethod([$class, $action], []);
        }
        r(url('docs/index'));
    }
    //验签
    public function checkSign(){
        $d = $this->post;
        if($d['partner']=='5300')
            return true;

        if( !isset($d['sign'])){
          err('[sign] Required parameter missing',300);
        }
        $sign = sign($d);
        if( !$sign==-1 ){
          err('[partner] Parameter error',300);
        }
        if( $sign!=strtoupper($d['sign'])){
          err('signature error',300);
        }
        return true;
    }
}
